<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;//加密
use Session;

class RegController extends Controller
{
    //注册页
    public function add()
    {
        $title = '个人注册';
        return view('home.login.reg',['title' => $title]);
    }
    //生成验证码
    public function createcode($id)
    {
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        Session::flash('code', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
    }
    //添加注册
    public function insert(Request $request)
    {
        //判断验证码
        $code = $request -> input('code');

        if($code != session('code'))
        {
            return back() -> with(['error' => '验证码错误']);
        }

        //获取添加页面提交上来的数据,并去除了'_token'隐藏的数据
        $data = $request -> except('_token');
        $this -> validate($request,[
            'username' => 'required|max:12|min:6',
            'password' => 'required|max:12|min:6',
            'repassword' => 'required|same:password',
            'email' => 'required|email',
            'phone' => 'required|regex:[1[0-9]{10}]|digits_between:11,11',

        ],[

            'username.required' => '用户名不能为空',
            'username.max' => '请输入6-12位的用户名',
            'username.min' => '请输入6-12位的用户名',
            'password.required' => '密码不能为空',
            'password.max' => '请输入6-12位的密码',
            'password.min' => '请输入6-12位的密码',
            'repassword.required' => '确认密码不能为空',
            'repassword.same' => '两次密码不一致,请重新输入',
            'email.required' => '邮箱地址不能为空',
            'email.email' => '请输入正确的email格式',
            'phone.required' => '手机号码不能为空',
            'phone.regex' => '请输入正确的手机号码',
            'phone.digits_between' => '请输入11位的有效手机号',
        ]);
        $data['atime'] = time();//添加用户注册事件;
        unset($data['repassword']);
        $request -> flash();
        //注册时间
        $data['password'] = Crypt::encrypt($data['password']);
        //设置用户状态的值
        $data['status'] = 1;
        $res = DB::table('bro_user') -> insert($data);

        if($res)
        {
            return redirect('/login');
        }else{
            return back();
        }


    }
    //用户名验证
    public function ajax(Request $request)
    {
        $username = $_GET['username'];
        $patten = '/\w{6,12}/';
        preg_match($patten,$username,$match);
        if($match)
        {
            $data = DB::table('bro_user') -> where('username',$username) -> first();
            if($data)
            {
                echo 1;//用户名已注册!
                exit;
            }else{
                echo 2;//用户名可以注册
                exit;
            }

        }else{
            echo 3;//用户名不合法!
            exit;
        }
    }
    //用户密码验证
    public function ajaxp()
    {
        $password = $_GET['password'];
        // echo $password;
        $patten = '/\w{6,12}/';


        preg_match($patten,$password,$matchpass);
        if($matchpass)
        {
            echo 1;
        }else{
            echo 2;
        }
    }
    //判断第二次
    public function ajaxrp()
    {
        $password = $_GET['password'];
        $repassword = $_GET['repassword'];

        $patten = '/\w{6,12}/';
        preg_match($patten,$repassword,$matchrp);

        if($matchrp)
        {
            if($repassword == '')
            {
                echo 3;//密码不能为空!
            }else{

                if($password == $repassword)
                {
                    echo 1;//亲~~两次密码输入一致,你的记性很好哇!
                }else{
                    echo 2;//两次密码输入不一致,请从新输入!
                }
            }
        }else{
            echo 4;//密码不合法!
        }
        
    }
    //邮箱验证
    public function email()
    {
        $eamil = $_GET['email'];
        $patten = '/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/';
        preg_match($patten,$eamil,$match);
        if($match)
        {
            echo 1;
        }else{
            echo 2;
        }
    }

}
